package com.example.demo.thread;

public class Demo4 {

    public static class MyThread implements Runnable {
        @Override
        public void run() {
            testMethod();
        }
    }

    public static class MyThread2 implements Runnable {
        @Override
        public void run() {
            testMethod();
        }
    }

    // 同步方法争夺锁
    public static synchronized void testMethod() {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
    }

    public static void main(String[] args) throws Exception {
        Thread a = new Thread(new MyThread());
        Thread b = new Thread(new MyThread2());

        a.start();
        a.join(500);
        //Thread.sleep(1000);
        b.start();

        //Thread.sleep(4000);

        System.out.println(a.getName() + ":" + a.getState());
        System.out.println(b.getName() + ":" + b.getState());

    }
}
